package com.yuanchu.order.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.order.dto.GoodsRefundParamsDto;
import com.yuanchu.order.dto.OrderRefundParamsDto;
import com.yuanchu.order.entity.YoshopOrderGoodsEntity;
import com.yuanchu.order.entity.YoshopOrderRefundEntity;
import com.yuanchu.order.service.YoshopOrderRefundService;
import com.yuanchu.order.service.YoshopOrderService;
import com.yuanchu.order.utils.SecurityUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-01-24 16:46
 */
@RestController
@RequestMapping("refund")
public class OrderRefundController {
    @Resource
    private YoshopOrderRefundService orderRefundService;
    @Resource
    private YoshopOrderService orderService;
    @GetMapping("/list")
    public R queryRefundOrder(@RequestParam("state") Integer state, @RequestParam("page") Integer page) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        PageResult<YoshopOrderRefundEntity> refundOrder = orderRefundService.queryRefundOrder(state, page, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", refundOrder);
        return R.ok().put("data", map);
    }
    @GetMapping("/detail2")
    public R queryRefundOrderDetail2(@RequestParam("orderRefundId") Integer orderRefundId) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        YoshopOrderRefundEntity refundOrder = orderRefundService.queryRefundOrderDetail(userId, orderRefundId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", refundOrder);
        return R.ok().put("data", map);
    }
    @GetMapping("/detail")
    public YoshopOrderRefundEntity queryOrderRefundDetail(@RequestParam("orderRefundId") Integer orderRefundId){
        return orderRefundService.queryRefundOrderDetail(orderRefundId);
    }

    // audit
    @PostMapping("/audit")
    public R auditOrderRefund(@RequestBody @Validated(ValidationGroups.Update.class) OrderRefundParamsDto param) {
        orderRefundService.auditOrderRefund(param);
        return R.ok();
    }

    // order/refund/goods
    @GetMapping("/goods")
    public R queryRefundOrderGoods(@RequestParam("orderGoodsId") Integer orderGoodsId) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        YoshopOrderGoodsEntity goods = orderService.queryOrderGoodsById(orderGoodsId);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "/order/refund/goods/apply", tags = "申请退款")
    @PostMapping("/apply")
    public R applyRefundOrderGoods(@RequestBody @Validated(ValidationGroups.Insert.class) GoodsRefundParamsDto param) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        orderRefundService.applyRefundOrderGoods(param, userId);
        return R.ok("提交成功");
    }

    @ApiOperation(value = "/order/refund/delivery", tags = "提交用户发货信息")
    @PostMapping("/delivery")
    public R refundOrderDelivery(@RequestBody @Validated(ValidationGroups.Update.class) OrderRefundParamsDto param) {
        // 根据dataType和page获取用戶订单列表数据
        Integer userId = SecurityUtil.getUserId();
        orderRefundService.refundOrderDelivery(param, userId);
        return R.ok("提交成功");
    }
}
